﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // Check Remember Cookies
            if (Request.Cookies["RememberCookie"] != null)
            {
                // Lay Cookies Account dang nhap
                txtUsername.Text = Server.HtmlEncode(Request.Cookies["RememberCookie"].Values["Username"]);
                txtPassword.Attributes.Add("Value", Server.HtmlEncode(Request.Cookies["RememberCookie"].Values["Password"]));
                chkRemember.Checked = Server.HtmlEncode(Request.Cookies["RememberCookie"].Values["Checkbox"]) == "True";
            }
            else
            {
                txtUsername.Text = "";
                txtPassword.Attributes.Add("Value", "");
                chkRemember.Checked = false;
            }
        }
    }
    protected void btnLogin_DirectClick(object sender, Ext.Net.DirectEventArgs e)
    {
        try
        {
            string username = txtUsername.Text.Trim();
            string password = txtPassword.Text;
            string passwordMd5 = UltisCode.ChangeMD5(password);

            if (username.Length == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Tên đăng nhập không để trống";
                lblMessage.IconCls = "#Error";
                lblMessage.IconAlign = Ext.Net.Alignment.Right;
                txtUsername.Focus(true, 0);
                return;
            }

            if (password.Length == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Mật khẩu nhập không để trống";
                lblMessage.IconCls = "#Error";
                lblMessage.IconAlign = Ext.Net.Alignment.Right;
                txtPassword.Focus(true, 0);
                return;
            }
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                try
                {
                    Adminstrastion admin = db.Adminstrastions.Single(ad => ad.Username == username);

                    if (passwordMd5.Equals(admin.Password))
                    {
                        // Lưu cookie
                        HttpCookie cookieRememberMe = new HttpCookie("RememberCookie");

                        // Lưu session
                        HttpContext.Current.Session["AdminLogined"] = admin;

                        if (chkRemember.Checked)
                        {
                            // Delete Cookie old if exists
                            if (Request.Cookies["RememberCookie"] != null)
                            {
                                cookieRememberMe.Values.Add("Username", null);
                                cookieRememberMe.Values.Add("Password", null);
                                cookieRememberMe.Values.Add("Checkbox", null);
                                cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                                Response.Cookies.Add(cookieRememberMe);
                            }
                            // Save cookies
                            cookieRememberMe.Values.Add("Username", username);
                            cookieRememberMe.Values.Add("Password", password);
                            cookieRememberMe.Values.Add("Checkbox", "True");
                            cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                            Response.Cookies.Add(cookieRememberMe);
                        }
                        else
                        {
                            // Delete Cookie old if exists
                            if (Request.Cookies["RememberCookie"] != null)
                            {
                                cookieRememberMe.Values.Add("Username", null);
                                cookieRememberMe.Values.Add("Password", null);
                                cookieRememberMe.Values.Add("Checkbox", null);
                                cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                                Response.Cookies.Add(cookieRememberMe);
                            }
                        }

                        Response.Redirect("Views/Default.aspx");
                    }
                    else
                    {
                        lblMessage.Visible = true;
                        lblMessage.Text = "Tài khoản hoặc mật khẩu không đúng";
                        lblMessage.IconCls = "#Error";
                        lblMessage.IconAlign = Ext.Net.Alignment.Right;
                    }
                }
                catch (Exception)
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "Tài khoản hoặc mật khẩu không đúng";
                    lblMessage.IconCls = "#Error";
                    lblMessage.IconAlign = Ext.Net.Alignment.Right;
                }

            }
        }
        catch (Exception)
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Hệ thống đang bận, xin bạn vui lòng quay lại sau";
        }
    }
}